package com.yss.kettle.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.yss.common.core.web.controller.BaseController;
import com.yss.common.core.web.domain.AjaxResult;
import com.yss.common.logger.annotation.Log;
import com.yss.common.logger.enums.BusinessType;
import com.yss.common.security.annotation.RequiresPermissions;
import com.yss.kettle.domain.JobParam;
import com.yss.kettle.service.IJobExecuteService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author lishuangliang
 * @since 2022-10-11
 */
@RestController
@RequestMapping("/jobExecute" )
public class JobExecuteController extends BaseController {

    @Autowired
    private IJobExecuteService jobExecuteService;


    /**
     * 执行作业分类信息
     *
     * @return
     */
    @RequiresPermissions("kettle:duties:execute" )
    @Log(title = "采集任务信息" , businessType = BusinessType.OTHER)
    @PostMapping(value = "/executeJobCategory")
    public AjaxResult executeJobCategory(@RequestBody String executeJson) throws Exception {
        Map<String, Object> mapType = JSON.parseObject(executeJson, LinkedHashMap.class);
        List<String> list = JSONArray.parseArray(mapType.get("ids" ).toString(), String.class);
        List<JobParam> listParam = null;
        if (StringUtils.isNotBlank(executeJson)) {
            listParam = JSONArray.parseArray(mapType.get("headList").toString(), JobParam.class);
        }
        jobExecuteService.execute(list, listParam, false, false);
        return success("执行作业分类信息成功！");
    }

    /**
     * 执行作业任务信息
     *
     * @return
     */
    @RequiresPermissions("kettle:task:execute" )
    @Log(title = "采集任务信息" , businessType = BusinessType.OTHER)
    @PostMapping(value = "/executeJobTask")
    public AjaxResult executeJobTask(@RequestBody String executeJson) throws Exception {
        Map<String, Object> mapType = JSON.parseObject(executeJson, LinkedHashMap.class);
        List<String> list = JSONArray.parseArray(mapType.get("ids" ).toString(), String.class);
        List<JobParam> listParam = null;
        if (StringUtils.isNotBlank(executeJson)) {
            listParam = JSONArray.parseArray(mapType.get("headList").toString(), JobParam.class);
        }
        jobExecuteService.execute(list, listParam, false, true);
        return success("执行作业任务信息成功！");
    }

    /**
     * 执行作业任务明细信息
     *
     * @return
     */
    @RequiresPermissions("kettle:details:execute" )
    @Log(title = "采集任务信息" , businessType = BusinessType.OTHER)
    @PostMapping(value = "/executeJobTaskDetail")
    public AjaxResult executeJobTaskDetail(@RequestBody String executeJson) throws Exception {
        Map<String, Object> mapType = JSON.parseObject(executeJson, LinkedHashMap.class);
        List<String> list = JSONArray.parseArray(mapType.get("ids" ).toString(), String.class);
        List<JobParam> listParam = null;
        if (StringUtils.isNotBlank(executeJson)) {
            listParam = JSONArray.parseArray(mapType.get("headList").toString(), JobParam.class);
        }
        jobExecuteService.execute(list, listParam, true, false);
        return success("执行作业任务明细信息成功！");
    }

   /* @Value("${file.uploadFolder}")
    private String path;*/

    /**
     * 导出kettle文件
     *
     * @return
     */
    /*@ApiOperation(value = "导出kettle文件")
    @RequestMapping(value = "/exportTemplateXml")
    @ResponseBody
    public RestResult exportTemplateXml(HttpServletResponse response) throws Exception {
        String fileName = "KETTLE_EXPORT_" + DateUtil.localDateToStrByFormat(LocalDate.now(), YssEnum.YSS_DEFAULT_DATE_FORMAT.getCode());
        jobExecuteService.exportTemplateXml(path, fileName, null);
        FileWriterUtil.downloadFile(response, path + fileName);
        return ResultGenerator.getSuccessResult("导出kettle文件成功！");
    }

    *//**
     * 导入kettle文件
     *
     * @return
     *//*


    @ApiOperation(value = "导入kettle文件")
    @RequestMapping(value = "/importTemplateXml")
    @ResponseBody
    public RestResult importTemplateXml(MultipartFile multipartFile) throws Exception {
        if (multipartFile.isEmpty()) {
            throw new BusinessException("导入文件为空，请稍后重试！");
        }
        File tempFile = FileUtil.createTempFile(new File(path));
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(), tempFile);
        jobExecuteService.importTemplateXml(path, tempFile.getName());
        return ResultGenerator.getSuccessResult("导入kettle文件成功！");
    }*/
}
